package com.huluobo.a2111douyindemo.home.tabItem;


import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.huluobo.a2111douyindemo.R;
import com.huluobo.a2111douyindemo.databinding.FragmentCommonBinding;
import com.huluobo.mvvm_base.mvvm.base.view.BaseMVVMFragment;
import com.huluobo.mvvm_base.mvvm.bean.LikeVideoBeanResult;
import com.huluobo.mvvm_base.mvvm.bean.VideoBeanResult;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class CommonFragment extends BaseMVVMFragment<FragmentCommonBinding, CommonViewModel> {

    //视频列表适配器
    private CommonAdapter commonAdapter;

    //视频列表数据源
    private List<VideoBeanResult.VideoBean> videoBeanList = new ArrayList<>();

    //banner数据源
    private List<String> banners = new ArrayList<>();

    private int currentPosition = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_common;
    }

    @Override
    protected void initData() {
        //读取banner
        viewModel.getBanner();
        //读取视频列表
        viewModel.getVideoList(1, 10);

        viewModel.getBannerSuccess.observe(this, bannerBeanResult -> {
            if (bannerBeanResult.getCode() == 200) {
                //banner的数据源只接收String类型的,才能直接展示,所以需要从数据源中取出所有的imagePath,重新填充一下
                for (int i = 0; i < bannerBeanResult.getData().size(); i++) {
                    banners.add(bannerBeanResult.getData().get(i).getImagePath());
                }
                dataBinding.homeItemBanner.setImages(banners);
                dataBinding.homeItemBanner.start();
            } else {
                ToastUtils.showLong(bannerBeanResult.getMessage());
            }
        });

        viewModel.getBannerFailed.observe(this, s -> {
            Log.i("TagA", "bannerFailed:" + s);
        });

        viewModel.getVideoSuccess.observe(this, videoBeanResult -> {
            if (videoBeanResult.getCode() == 200) {
                videoBeanList.clear();
                videoBeanList.addAll(videoBeanResult.getData());
                commonAdapter.notifyDataSetChanged();
            } else {
                ToastUtils.showLong(videoBeanResult.getMessage());
            }
        });

        viewModel.getVideoFailed.observe(this, s -> {
            Log.i("TagA", "videosFailed:" + s);
        });

        viewModel.likeSuccess.observe(this, likeVideoBeanResult -> {
            ToastUtils.showLong(likeVideoBeanResult.getMessage());
            //更新数据源中的数据,修改点赞
            videoBeanList.get(currentPosition).setDianzan(1);
            //单独刷新适配器
            commonAdapter.notifyItemChanged(currentPosition);
        });

        viewModel.likeFailed.observe(this, s -> {
            Log.i("TagA", s);
        });

        viewModel.noLikeSuccess.observe(this, likeVideoBeanResult -> {
            ToastUtils.showLong(likeVideoBeanResult.getMessage());
            //更新数据源中的数据,修改点赞
            videoBeanList.get(currentPosition).setDianzan(0);
            //单独刷新适配器
            commonAdapter.notifyItemChanged(currentPosition);
        });

        viewModel.noLikeFailed.observe(this, s -> {
            Log.i("TagA", s);
        });
    }

    @Override
    protected void initView() {
        //初始化视频列表
        dataBinding.homeItemRv.setLayoutManager(new GridLayoutManager(requireContext(), 2));
        commonAdapter = new CommonAdapter(videoBeanList);
        dataBinding.homeItemRv.setAdapter(commonAdapter);

        //初始化banner
        dataBinding.homeItemBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);//设置banner指示器,当前为圆点指示器
        //设置banner的图片加载器
        dataBinding.homeItemBanner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
//                Glide.with(context).load(path.toString()).into(imageView);
                //由于banner中有个同名的ImageLoader,所以我们自己的ImageLoader需要单独导包的形式来调用,否则会冲突
                com.huluobo.a2111douyindemo.utils.ImageLoader.getInstance().loadImage(context, path.toString(),
                        imageView);
            }
        });
        dataBinding.homeItemBanner.setDelayTime(1500);//设置banner的延迟时间
        dataBinding.homeItemBanner.setIndicatorGravity(BannerConfig.CENTER);//设置指示器的位置

        //给item中的按钮(控件)添加点击事件
        commonAdapter.addChildClickViewIds(R.id.item_video_like);
        //设置点击事件
        commonAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                currentPosition = position;
                //点击到了点赞按钮
                if (view.getId() == R.id.item_video_like) {
                    likeVideo(videoBeanList.get(position));
                }
            }
        });
    }

    //视频点赞的方法
    private void likeVideo(VideoBeanResult.VideoBean videoBean) {
        if (videoBean.getDianzan() == 0) {
            //代表未点赞
            viewModel.likeVideo(videoBean.getId());
        } else {
            //代表已点赞
            viewModel.noLikeVideo(videoBean.getId());
        }
    }

    @Override
    protected Class<CommonViewModel> getViewModel() {
        return CommonViewModel.class;
    }
}